package org.example.tlias.services.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import org.example.tlias.mapper.ClazzMapper;
import org.example.tlias.pojo.Clazz;
import org.example.tlias.pojo.ClazzQueryParam;
import org.example.tlias.pojo.PageResult;
import org.example.tlias.services.ClazzsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.util.List;

@Service
public class ClazzsServiceImpl implements ClazzsService {
    @Autowired
    private ClazzMapper clazzMapper;
    @Override
    public PageResult<Clazz> page(ClazzQueryParam clazzQueryParam) {

        PageHelper.startPage(clazzQueryParam.getPage(),clazzQueryParam.getPageSize());
        List<Clazz> clazzList =  clazzMapper.page(clazzQueryParam);
        Page<Clazz> clazzPage = (Page<Clazz>) clazzList;
        return new PageResult<Clazz>(clazzPage.getTotal(),clazzPage.getResult());
    }

    @Override
    public void save(Clazz clazz) {
        clazz.setCreateTime(LocalDateTime.now());
        clazz.setUpdateTime(LocalDateTime.now());
        clazzMapper.save(clazz);
    }

    @Override
    public void deleteById(Integer id) {
        clazzMapper.deleteById(id);
    }

    @Override
    public Clazz getById(Integer id) {
        Clazz clazz = clazzMapper.getById(id);
        LocalDate now = LocalDate.now();
        if (now.isAfter(clazz.getEndDate())){
            clazz.setStatus("已结课");
        }else if(now.isAfter(clazz.getBeginDate()) && now.isBefore(clazz.getEndDate())){
            clazz.setStatus("进行中");
        }else{
            clazz.setStatus("未开始");
        }
        return clazz ;
    }

    @Override
    public void update(Clazz clazz) {
        clazzMapper.update(clazz);
    }

    @Override
    public List<Clazz> list() {
        return clazzMapper.list();
    }
}
